<?php namespace app\api\controller;

use app\common\controller\AuthAndBind;
use think\Controller;
use think\Db;
use alisms\SendSms;
use esign\EsignHandle;

/**
 * api
 */
class PcIndex extends AuthAndBind{
	
	public function __construct(){
		parent::__construct();
	}
	
	/**
	 * pc首页村落销售量折线图统计
	 */
	public function villageSalesVolumelineChart(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				$shopData = [];
				if( $userInfo['info']['type'] == 3 ){
					$shopData = Db::table("gd_shop") -> where(['create_user_id'=>$userInfo['info']['id']]) -> find();
				}
				
//				订单
				if( !empty($shopData) ) $where[] = ['shop_id', 'eq', $shopData['id']];
				$where[] = ['is_delete', 'eq', 0];
				$where[] = ['village_id', 'gt', 0];
//				$where[] = ['create_date', 'between', [date("Y-m-d 00:00:00", strtotime($startDay)), $endDay]];
				$field = ['village_id', 'village_name', 'count(id)'=>'countNums'];
				$data = Db::table("gd_order") -> field($field) -> where($where) -> group("village_id") -> order("countNums desc") -> limit(0, 10) -> select();
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'pc首页村落销售量折线图统计',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * pc首页订单销售量折线图统计
	 */
	public function orderSalesVolumelineChart(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				$shopData = [];
				if( $userInfo['info']['type'] == 3 ){
					$shopData = Db::table("gd_shop") -> where(['create_user_id'=>$userInfo['info']['id']]) -> find();
				}
				
//				订单
				$where[] = ['is_delete', 'eq', 0];
				if( !empty($shopData) ) $where[] = ['shop_id', 'eq', $shopData['id']];
//				$where[] = ['create_date', 'between', [date("Y-m-d 00:00:00", strtotime($startDay)), $endDay]];
				$field = ['goods_id', 'goods_name', 'count(id)'=>'countNums'];
				$data = Db::table("gd_order") -> field($field) -> where($where) -> group("goods_id") -> order("countNums desc") -> limit(0, 10) -> select();
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'pc首页订单销售量折线图统计',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * pc首页订单折线图统计
	 */
	public function orderlineChart(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'dayNums'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				$shopData = [];
				if( $userInfo['info']['type'] == 3 ){
					$shopData = Db::table("gd_shop") -> where(['create_user_id'=>$userInfo['info']['id']]) -> find();
				}
				
				$startDay = '-' . ($in['dayNums']-1) . ' day';
				$endDay = date("Y-m-d 23:59:59", time());
				$date = getDateList2($startDay, $endDay);
				$data = [];
				foreach ($date as $k => $v) {
					$data[]['date'] = $v;
				}
				
//				订单
				if( !empty($shopData) ) $where[] = ['shop_id', 'eq', $shopData['id']];
				$where[] = ['is_delete', 'eq', 0];
				$where[] = ['create_date', 'between', [date("Y-m-d 00:00:00", strtotime($startDay)), $endDay]];
				$field = ["DATE_FORMAT(create_date, '%Y-%m-%d')"=>'create_date', 'count(id)'=>'countNums', 'sum(actual_price)'=>'sumActualPrice'];
				$order = Db::table("gd_order") -> field($field) -> where($where) -> group("DATE_FORMAT(create_date, '%Y-%m-%d')") -> select();
				
				foreach ($data as $k => $v) {
					$data[$k]['countNums'] = 0;
					$data[$k]['sumActualPrice'] = 0;
					if( !empty($order) ){
						foreach ($order as $kk => $vv) {
							if( $v['date'] == $vv['create_date'] ) {
								$data[$k]['countNums'] = $vv['countNums'];
								$data[$k]['sumActualPrice'] = $vv['sumActualPrice'];
							}
						}
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'pc首页订单折线图统计',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * pc首页订单商品数折线图统计
	 */
	public function orderGoodsNumslineChart(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'dayNums'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$startDay = '-' . ($in['dayNums']-1) . ' day';
				$endDay = date("Y-m-d 23:59:59", time());
				$date = getDateList2($startDay, $endDay);
				$data = [];
				foreach ($date as $k => $v) {
					$data[]['date'] = $v;
				}
				
				$shopData = [];
				if( $userInfo['info']['type'] == 3 ){
					$shopData = Db::table("gd_shop") -> where(['create_user_id'=>$userInfo['info']['id']]) -> find();
				}
				
				
//				订单数
				if( !empty($shopData) ) $where[] = ['shop_id', 'eq', $shopData['id']];
				$where[] = ['is_delete', 'eq', 0];
				$where[] = ['create_date', 'between', [date("Y-m-d 00:00:00", strtotime($startDay)), $endDay]];
				$field = ["DATE_FORMAT(create_date, '%Y-%m-%d')"=>'create_date', 'sum(num)'];
				$order = Db::table("gd_order") -> field($field) -> where($where) -> group("DATE_FORMAT(create_date, '%Y-%m-%d')") -> select();
				
				foreach ($data as $k => $v) {
					$data[$k]['sumNums'] = 0;
					if( !empty($order) ){
						foreach ($order as $kk => $vv) {
							if( $v['date'] == $vv['create_date'] ) $data[$k]['sumNums'] = $vv['sum(num)'];
						}
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'pc首页订单商品数折线图统计',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * pc首页统计
	 */
	public function statistics(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$shopData = [];
				if( $userInfo['info']['type'] == 3 ){
					$shopData = Db::table("gd_shop") -> where(['create_user_id'=>$userInfo['info']['id']]) -> find();
				}
				
//				订单数
				if( !empty($shopData) ) $where1[] = ['shop_id', 'eq', $shopData['id']];
				$where1[] = ['is_delete', 'eq', 0];
				$data1 = Db::table("gd_order") -> where($where1) -> count();

//				上架商品数
				if( !empty($shopData) ) $where2[] = ['shop_id', 'eq', $shopData['id']];
				$where2[] = ['is_delete', 'eq', 0];
				$where2[] = ['status', 'eq', 1];
				$data2 = Db::table("gd_goods") -> where($where2) -> count();
				
//				下架商品数
				if( !empty($shopData) ) $where3[] = ['shop_id', 'eq', $shopData['id']];
				$where3[] = ['is_delete', 'eq', 0];
				$where3[] = ['status', 'eq', 0];
				$data3 = Db::table("gd_goods") -> where($where3) -> count();
				
//				今日新增会员
//				if( !empty($shopData) ) $where4[] = ['shop_id', 'eq', $shopData['id']];
				$where4[] = ['is_delete', 'eq', 0];
				$where4[] = ['create_date', 'between', [date("Y-m-d 00:00:00", time()), date("Y-m-d 23:59:59", time())]];
				$data4 = Db::table("gd_user") -> where($where4) -> count();
				
//				昨日新增会员
//				if( !empty($shopData) ) $where5[] = ['shop_id', 'eq', $shopData['id']];
				$where5[] = ['is_delete', 'eq', 0];
				$where5[] = ['create_date', 'between', [date("Y-m-d 00:00:00", strtotime("-1 day")), date("Y-m-d 23:59:59", strtotime("-1 day"))]];
				$data5 = Db::table("gd_user") -> where($where5) -> count();
				
//				最近7天新增会员
//				if( !empty($shopData) ) $where6[] = ['shop_id', 'eq', $shopData['id']];
				$where6[] = ['is_delete', 'eq', 0];
				$where6[] = ['create_date', 'between', [date("Y-m-d 00:00:00", strtotime("-6 day")), date("Y-m-d 23:59:59", time())]];
				$data6 = Db::table("gd_user") -> where($where6) -> count();
				
//				最近一月新增会员
//				if( !empty($shopData) ) $where7[] = ['shop_id', 'eq', $shopData['id']];
				$where7[] = ['is_delete', 'eq', 0];
				$where7[] = ['create_date', 'between', [date("Y-m-d 00:00:00", strtotime("-1 month")), date("Y-m-d 23:59:59", time())]];
				$data7 = Db::table("gd_user") -> where($where7) -> count();
				
//				会员总数
				$where8[] = ['is_delete', 'eq', 0];
//				if( !empty($shopData) ) $where8[] = ['shop_id', 'eq', $shopData['id']];
				$data8 = Db::table("gd_user") -> where($where8) -> count();	
				
				$data['data1'] = $data1;
				$data['data2'] = $data2;
				$data['data3'] = $data3;
				$data['data4'] = $data4;
				$data['data5'] = $data5;
				$data['data6'] = $data6;
				$data['data7'] = $data7;
				$data['data8'] = $data8;
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'pc首页统计',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
}

?>